﻿using System;
using System.Text;
using AutoMapper;
using Microsoft.Extensions.Configuration;
using BestTrader.Cms.IServices;
using BestTrader.Cms.ViewModels;
using BestTrader.Cms.ViewModels.WebApi;
using Microsoft.AspNetCore.Mvc;
using BestTrader.Cms.Core.Extensions;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace BestTrader.Cms.WebApi.Controllers.v1
{
    /// <summary>
    /// 其他管理
    /// </summary>
    [ApiVersion("1.0")]
    [Route("api/v{version:apiVersion}/[controller]")]
    [ApiController]
    public class ItemsController : BaseController
    {
        private readonly IQiniuService _qiniuService;
        private readonly IManagerService _managerService;
        private readonly IMapper _mapper;
        private readonly IConfiguration _configuration;


        public ItemsController(IQiniuService qiniuService, IManagerService managerService, IMapper mapper, IConfiguration configuration)
        {
            _qiniuService = qiniuService;
            _managerService = managerService;
            _mapper = mapper;
            _configuration = configuration;
        }

        /// <summary>
        ///[10001] 获取七牛云令牌
        /// </summary>
        [HttpGet("GetToken")]
        public JsonResult GetToken()
        {
            var _token = _qiniuService.GetToken();
            return Json<Object>(_token);
        }

        /// <summary>
        /// [10002] 检查版本更新
        /// 
        /// </summary>
        /// <remarks>
        /// output /
        /// {
        /// IsNewVersion:"" //0 没有新版  1：新版本  2：强制更新 
        /// ClientType:"", //客户端类型  Android ||  ios
        /// UpdateText:"",//更新内容
        /// DownloadPath:""  //下载路径
        /// 
        /// }
        /// </remarks>
        /// <returns></returns>
        [HttpPost("GetCheckUpdate")]
        public JsonResult GetCheckUpdate(CheckUpdateInput input)
        {
            var result = new BaseResult();
            var host = Request.Host.ToString();
           var scheme =  Request.Scheme;
           var data = new CheckUpdateOutput
            {
                IsNewVersion = 0,
                ClientType = "",
                DownloadPath = "",
                UpdateText = ""
            };
            string currentVersion, IsNewVersion, ClientType;
            //TODO:: http
            if (input.ClientType == "Android")
            {
                currentVersion = _configuration["ClientApp:Android:CurrentVersion"];
                IsNewVersion = _configuration["ClientApp:Android:IsNewVersion"];
                ClientType = _configuration["ClientApp:Android:ClientType"];
                if (input.CurrentVersion !=currentVersion) {
                   data = new CheckUpdateOutput
                    {
                        IsNewVersion = IsNewVersion.ToInt32(),
                        ClientType = "Android",
                        DownloadPath = $"{scheme}://{host}/app.apk",
                        UpdateText =  "1.更新内容更新内容；\n2.更新内容啦更新内容"

                   };
                }
            }
           
            return Json<CheckUpdateOutput>(data, result.Success, result.ResultCode, result.ResultMsg);

        }

        ///// <summary>
        ///// 相关集合
        ///// </summary>
        //[HttpGet("{enCode}")]
        //public JsonResult GetItem(string enCode)
        //{
        //    var obj = new
        //    {
        //        Name = "张三"
        //    };
        //    return Json<Object>(obj);
        //}

        ///// <summary>
        ///// 相关集合,以“，”分割
        ///// </summary>
        //[ActionName("All")]
        //[HttpGet("[action]/{enCodes}")]
        //public void GetItems(string enCodes)
        //{

        //}

    }
}
